package com.spark.lola.controller.purchaser;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spark.lola.logistics.entity.CarSubscribe;
import com.spark.lola.logistics.service.CarSubscribeService;
import com.spark.lola.support.api.entity.EnterpriseTokenUser;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.mybatis.common.QueryCondition;
import com.spark.lola.support.mybatis.entity.QueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author: Zhi-peng wang
 * @date: 2020/05/25 14:54
 **/
@RestController
@RequestMapping("/api/pur/car-subscribe")
@Api(value = "/api/pur/car-subscribe", tags = "回头车预订接口")
public class CarSubscribeController {

    @Autowired
    private CarSubscribeService carSubscribeService;

    @ApiOperation(value = "分页查询回头车预订信息")
    @RequestMapping(value = "/findBy", method = RequestMethod.POST)
    public ResponseBean findPage(@RequestBody QueryParam<CarSubscribe> queryParam) {
        QueryCondition queryCondition = new QueryCondition();
        queryCondition.setKey("o.used");
        queryCondition.setOper("=");
        queryCondition.setValue(CommonConstant.W);
        queryParam.getQueryList().add(queryCondition);
        IPage<CarSubscribe> carSubscribeIPage = carSubscribeService.findPage(queryParam);
        return ResponseBean.success(carSubscribeIPage);
    }

    @ApiOperation(value = "查询回头已预约回头车")
    @RequestMapping(value = "/findPageSub", method = RequestMethod.POST)
    public ResponseBean findPageSub(@SessionAttribute EnterpriseTokenUser<?> tokenUser) {
        QueryParam<CarSubscribe> queryParam = new QueryParam<>();
        queryParam.setPageIndex(1);
        queryParam.setPageSize(100);
        queryParam.setQueryList(new ArrayList<>());
        QueryCondition queryCondition = new QueryCondition();
        queryCondition.setKey("o.used");
        queryCondition.setOper("=");
        queryCondition.setValue(CommonConstant.Y);
        queryParam.getQueryList().add(queryCondition);

        QueryCondition queryCondition1 = new QueryCondition();
        queryCondition1.setKey("o.subscribeId");
        queryCondition1.setOper("=");
        queryCondition1.setValue(tokenUser.getShiroUser().getId());
        queryParam.getQueryList().add(queryCondition1);
        IPage<CarSubscribe> carSubscribeIPage = carSubscribeService.findPage(queryParam);
        return ResponseBean.success(carSubscribeIPage);
    }

    @ApiOperation(value = "获取回头车预订信息详情")
    @ApiImplicitParam(name = "id", value = "回头车id", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/findInfo/{id}", method = RequestMethod.POST)
    public ResponseBean findInfo(@PathVariable long id) {
        CarSubscribe carSubscribe = new CarSubscribe();
        if (id != 0) {
            carSubscribe = carSubscribeService.getById(id);
        }
        return ResponseBean.success(carSubscribe);
    }

    @ApiOperation(value = "预订回头车")
    @RequestMapping(value = "/subscriber", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean subscriber(@RequestBody JSONObject jsonObject, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        Long id = jsonObject.getLong("id");
        String remark = jsonObject.getString("remark");
        if (id == null) {
            return ResponseBean.error("参数为空");
        }
        carSubscribeService.subscriber(id, remark, tokenUser.getShiroUser().getId(),
                tokenUser.getShiroUser().getRealName(), tokenUser.getShiroUser());
        return ResponseBean.success("预订成功");
    }

    @ApiOperation(value = "取消预订")
    @ApiImplicitParam(name = "id", value = "回头车id", required = true, dataType = "long", paramType = "path")
    @RequestMapping(value = "/cancel/{id}", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean cancel(@PathVariable long id, @SessionAttribute EnterpriseTokenUser<?> tokenUser) throws Exception {
        carSubscribeService.cancel(id, tokenUser.getShiroUser().getId());
        return ResponseBean.success("取消预订成功");
    }

}
